﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GONES.IBLL;
using GONES.IDAL;
using GONES.Model;
using log4net;
using System.Reflection;
using Newtonsoft.Json;
using GONES.Tools.Common;
using System.Collections;

namespace GONES.BLL
{
    public class K_CustomersManager : IK_CustomersManager
    {
        protected static readonly ILog Log =
                LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        private readonly IUserInfo _userInfo;
        public IK_CustomersDao K_CustomersDao;
        public IS_LogDao S_LogDao;
        public S_LogManager S_LogManager;

        public string ErrMsg { get; set; }

        public K_CustomersManager(IUserInfo userinfo)
        {
            _userInfo = userinfo;
        }
        public K_Customers Get(string id)
        {
            return K_CustomersDao.Get(id);
        }

        /// <summary>
        /// 新增客户信息
        /// </summary>
        /// <param name="users">客户信息</param>
        public void Add(K_Customers customers)
        {
            if (IsError(customers))
                return;
            customers.CreateTime = System.DateTime.Now; ;
            K_CustomersDao.Save(customers);
            SaveLog("客户管理", "客户添加", "K3基础资料", customers); 
        }

        /// <summary>
        /// 修改客户信息
        /// </summary>
        /// <param name="users">客户信息</param>
        public void Edit(K_Customers customers)
        {
            if (IsError(customers))
                return;
            customers.ModifyTime = System.DateTime.Now; 
            K_CustomersDao.Update(customers);
            SaveLog("客户管理", "客户修改","K3基础资料",customers); 
        }

        /// <summary>
        /// 删除客户信息
        /// </summary>
        /// <param name="id">客户ID</param>
        public void Delete(string id)
        {
            K_Customers customers = K_CustomersDao.Get(id);
            K_CustomersDao.Delete(customers);
            SaveLog( "客户管理", "客户删除","K3基础资料",customers); 
        }

        /// <summary>
        /// 获取客户列表
        /// </summary>
        /// <param name="filters">条件</param>
        /// <param name="pageIndex">页号</param>
        /// <param name="pageSize">每页条数</param>
        /// <param name="recordCount">记录条数</param>
        /// <returns>客户列表</returns>
        public IList<K_Customers> List(IList<DataFilter> filters, int pageIndex, int pageSize, ref long recordCount)
        {
            string strFilter = DataOperate.GetHqlstrByExtFilter(filters, "e");

            recordCount = K_CustomersDao.ExecHqlResultLong("SELECT COUNT(*) FROM K_Customers e WHERE 1=1 " +
                                                  (string.IsNullOrEmpty(strFilter) ? string.Empty : " AND " + strFilter));

            return K_CustomersDao.GetHqlList("SELECT e FROM K_Customers e WHERE 1=1 " +
                                          (string.IsNullOrEmpty(strFilter)
                                               ? string.Empty
                                               : " AND " + strFilter) + " ORDER BY e.ID DESC", (pageIndex - 1) * pageSize,
                                          pageSize);
        }

        private void SaveLog(string module, string operate, string logtype, K_Customers customers)
        {
            S_LogManager = new S_LogManager(_userInfo);
            string detail = string.Format("{0}:客户名是{1}", operate, customers.Name);
            SaveLog(module, operate, logtype, detail);
            if (Log.IsInfoEnabled)
                Log.Info(string.Format("{0}：{1}", operate, JsonConvert.SerializeObject(customers)));
        }
        
        protected bool IsError(K_Customers customers)
        {
            if (string.IsNullOrEmpty(customers.Name))
            {
                ErrMsg = "客户姓名不能为空！";
            }
            if (!string.IsNullOrEmpty(ErrMsg))
                return true;
            return false;
        }

        /// <summary>
        /// 保存日志
        /// </summary>
        /// <param name="module">模块名称</param>
        /// <param name="operate">操作名称</param>
        /// <param name="logtype">日志类型（K3基础资料、基础资料等）</param>
        /// <param name="detail">详细日志</param>
        protected void SaveLog(string module, string operate, string logtype, string detail)
        {
            try
            {
                S_LogDao.Save(new S_Log
                {
                    ID = Rnd.Guid.ToString(),
                    Module = module,
                    Operate = operate,
                    Detail = detail,
                    LogType = logtype,
                    OpDate = DateTime.Now,
                    CreateUser = _userInfo.CLoginInfo.UserName,
                    CreateTime = DateTime.Now,
                    ModifyUser = _userInfo.CLoginInfo.UserName,
                    ModifyTime = DateTime.Now,
                    Ip = Tools.Common.Tools.GetIP()
                });
            }
            catch
            {
            }
        }
    }
}
